package dq.stduy.jax;

import dq.stduy.jax.element.Message;
import dq.stduy.jax.element.MyBody;
import dq.stduy.jax.element.MyFile;
import org.apache.commons.text.StringSubstitutor;
import org.springframework.cglib.beans.BeanMap;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

/**
 * @author dongqun
 * @version 1.0
 * @description 类的功能描述
 * @date 2019/9/26 18:47
 */
public class JAXBUtil {

    public static String marshaller(Object object){
        try {
            StringWriter writer = new StringWriter();
            JAXBContext context = JAXBContext.newInstance(object.getClass());
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
            marshaller.marshal(object,writer);
            return writer.toString();
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void marshaller(Object object,String path){
        try {
            OutputStream is = new FileOutputStream(path);

            JAXBContext context = JAXBContext.newInstance(object.getClass());
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
            marshaller.marshal(object,is);
        } catch (JAXBException | FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void marshaller(Object object,OutputStream os){
        try {
            JAXBContext context = JAXBContext.newInstance(object.getClass());
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
            marshaller.marshal(object,os);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

    public static Object unMarshaller(String path,Class cls){
        try {
            File file = new File(path);
            JAXBContext context = JAXBContext.newInstance(cls);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            return unmarshaller.unmarshal(file);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static Object unMarshaller2(String xml,Class cls){
        try {
            StringReader reader = new StringReader(xml);
            JAXBContext context = JAXBContext.newInstance(cls);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            return unmarshaller.unmarshal(reader);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String test(){
        Flight flightInfo = new Flight();
        flightInfo.setFlightNo("CS121212");
        flightInfo.setOther("其他的是是是");
        String boardGuideMessage = "您关注的${flightNo:---}航班计划于两小时内起飞，请您留意登机引导信息。";
        BeanMap beanMap = BeanMap.create(flightInfo);
        StringSubstitutor substitutor = new StringSubstitutor(beanMap);
        return boardGuideMessage = substitutor.replace(boardGuideMessage);
    }





    public static void main(String[] args){
        String test = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Message><header>我是消息头</header><body><time>1990-09-09 11:23:34:999</time><sender>卡尔</sender><receiver>aaa</receiver><receiver_list><receiver>ss</receiver><receiver>sss</receiver><receiver>sssss</receiver></receiver_list><attach><file_path>/火星情报局/大中华司</file_path></attach></body></Message>";
        Message message = new Message();
        message.setHeader("我是消息头");
        MyBody myBody = new MyBody();
        MyFile myFile = new MyFile();
        myFile.setPath("/火星情报局/大中华司");
        myBody.setFile(myFile);
        myBody.setReceiver("aaa");
        String[] receive = {"ss","sss","sssss"};
        myBody.setReceivers(Arrays.asList(receive));
        myBody.setSender("卡尔");
        myBody.setNote("备注信息");
        myBody.setTime("1990-09-09 11:23:34:999");
        message.setBody(myBody);
//        System.out.println(marshaller(message));
//        marshaller(message,"./message.xml");
//        marshaller(message,System.out);
//        System.out.println(unMarshaller("message.xml",Message.class));
//        System.out.println(unMarshaller2(test,Message.class));
        System.out.println(test());
    }



}
